package com.bs.process.project.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bs.common.annotation.Log;
import com.bs.common.enums.BusinessType;
import com.bs.process.project.domain.SysProjectFileType;
import com.bs.process.project.service.ISysProjectFileTypeService;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.utils.StringUtils;
import com.bs.common.utils.poi.ExcelUtil;
import com.bs.framework.util.ShiroUtils;
import com.bs.common.core.page.TableDataInfo;

/**
 * 项目文件类型Controller
 *
 * @author Xianlu Tech
 * @date 2020-06-13
 */
@Controller
@RequestMapping("/process/projectfiletype")
public class SysProjectFileTypeController extends BaseController {
    private String prefix = "process/project/type";
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
    @Autowired
    private ISysProjectFileTypeService sysProjectFileTypeService;

    @RequiresPermissions("process:type:view")
    @GetMapping("/type")
    public String type() {
        return prefix + "/type";
    }

    /**
     * 校验菜单名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(SysProjectFileType sysProjectFileType) {
        return sysProjectFileTypeService.checkTypeNameUnique(sysProjectFileType);
    }

    /**
     * 查询项目文件类型列表
     */
    @RequiresPermissions("process:type:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysProjectFileType> list(SysProjectFileType sysProjectFileType) {
        List<SysProjectFileType> list = sysProjectFileTypeService.selectSysProjectFileTypeList(sysProjectFileType);
        return list;
    }

    /**
     * 导出项目文件类型列表
     */
    @RequiresPermissions("process:type:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysProjectFileType sysProjectFileType) {
        List<SysProjectFileType> list = sysProjectFileTypeService.selectSysProjectFileTypeList(sysProjectFileType);
        ExcelUtil<SysProjectFileType> util = new ExcelUtil<SysProjectFileType>(SysProjectFileType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增项目文件类型
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") String parentId, ModelMap mmap) {
        SysProjectFileType projectType = null;
        if (!parentId.equalsIgnoreCase("0")) {
            projectType = sysProjectFileTypeService.selectSysProjectFileTypeById(parentId);
        } else {
//            String code = "FT-" + format.format(new Date());
            projectType = new SysProjectFileType();
            projectType.setId(null);
            projectType.setTypename("主目录");
        }
        mmap.put("projectType", projectType);
        return prefix + "/add";
    }

    /**
     * 新增保存项目文件类型
     */
    @RequiresPermissions("process:type:add")
    @Log(title = "项目文件类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysProjectFileType sysProjectFileType) {
        String code = "FT-" + format.format(new Date());
        sysProjectFileType.setId(code);
        return toAjax(sysProjectFileTypeService.insertSysProjectFileType(sysProjectFileType));
    }

    /**
     * 修改项目文件类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        SysProjectFileType sysProjectFileType = sysProjectFileTypeService.selectSysProjectFileTypeById(id);
        mmap.put("sysProjectFileType", sysProjectFileType);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目文件类型
     */
    @RequiresPermissions("process:type:edit")
    @Log(title = "项目文件类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysProjectFileType sysProjectFileType) {
        return toAjax(sysProjectFileTypeService.updateSysProjectFileType(sysProjectFileType));
    }

    /**
     * 删除项目文件类型
     */
    @RequiresPermissions("process:type:remove")
    @Log(title = "项目文件类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysProjectFileTypeService.deleteSysProjectFileTypeByIds(ids));
    }

    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult removeById(@PathVariable("id") String id) {
        if (sysProjectFileTypeService.selectCountSysProjectFileTypeByParentId(id) > 0) {
            return AjaxResult.warn("存在子菜单,不允许删除");
        }
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(sysProjectFileTypeService.deleteSysProjectFileTypeById(id));
    }

    @RequestMapping("/projectfilename")
    @ResponseBody
    public AjaxResult projectfilename(String parentId) {
        SysProjectFileType sysProjectFileType = new SysProjectFileType();
        sysProjectFileType.setParentId(parentId);

        List<SysProjectFileType> sysProjectFileTypes = sysProjectFileTypeService.selectSysProjectFileTypeList(sysProjectFileType);
        return AjaxResult.success(sysProjectFileTypes);
    }
}
